package com.cmcc.aregister.cs.asntask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.cmcc.aregister.cs.asntask.InterfaceUtils.Ad_bar;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.Click_ad_listener;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.Liuliang_tongji;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.model.AdModel;
import com.cmcc.aregister.cs.model.AdbarResultModel;
import com.cmcc.aregister.cs.model.ClickAdResultModel;
/**
 * 异步请求
 * @author ld 20130325
 *
 */
public class MyAsynTaskOther extends AsyncTask<String, Integer, String> {
	private Ad_bar mAd_bar;
	private Liuliang_tongji mLiuliang_tongji;
	private Click_ad_listener mClick_ad_listener;
	private String result_code;
	private JSONObject jsonObject;
	Map<String, String> map;
	int code;
	private AdbarResultModel mAdbarResultModel;
	private ClickAdResultModel mClickAdResultModel;
	
	/**
	 * 参数初始化
	 * @param code
	 * @param map
	 */
	public void initMyAsynaTask(int code,Map<String, String> map,JSONObject j){
		this.map = map;
		this.code = code;
		this.jsonObject = j;
	}
	
	public MyAsynTaskOther(Liuliang_tongji mDataCallback,int code,Map<String, String> map,JSONObject j) {
		// TODO Auto-generated constructor stub
		initMyAsynaTask(code,map,j);
		this.mLiuliang_tongji = mDataCallback;
	}
	public MyAsynTaskOther(Ad_bar mAd_bar,int code,Map<String, String> map) {
		// TODO Auto-generated constructor stub
		this.map = map;
		this.code = code;
		this.mAd_bar = mAd_bar;
	}
	
	public MyAsynTaskOther(Click_ad_listener mClick_ad_listener,int code,Map<String, String> map) {
		// TODO Auto-generated constructor stub
		this.map = map;
		this.code = code;
		this.mClick_ad_listener = mClick_ad_listener;
	}
	/**
	 * 后台异步请求数据 
	 */
	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		switch (code) {
		case GlobalSetting.LIULIANG_TONGJI:
			result_code = ConfigUtilHelper.liuliangRequestPost(Arguments.LIULIANG_URL, map,jsonObject);
			break;
		case GlobalSetting.AD_BAR:
			result_code = ConfigUtilHelper.requestOther(Arguments.AD_BAR_URL, map);
			break;
		case GlobalSetting.CLICK_AD_BAR:
			result_code = ConfigUtilHelper.requestOther(Arguments.AD_BAR_URL, map);
			break;
		default:
			break;
		}
		
		return null;
	}
	/**
	 * 执行结果
	 */
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		switch (code) {
		case GlobalSetting.LIULIANG_TONGJI:
			mLiuliang_tongji.liuliang_callback(result_code);
			break;
		case GlobalSetting.AD_BAR:
			mAdbarResultModel = new AdbarResultModel();
			List<AdModel> ads = new ArrayList<AdModel>();
			try{
				JSONObject json = new JSONObject(result_code);
				mAdbarResultModel.resultCode = json.getString("resultCode");
				mAdbarResultModel.resultMsg = json.getString("resultMsg");
				JSONArray array = json.getJSONArray("ads");
 				for (int i = 0; i < array.length(); i++) {
 					AdModel ad = new AdModel();
 					JSONObject item = (JSONObject) array.opt(i);
 					ad.content = item.getString("content");
 					ad.id = item.getString("id");
 					ad.mode = item.getString("mode");
 					ad.pic = item.getString("pic");
 					ad.playtime = item.getString("playtime");
 					ad.scantype = item.getString("scantype");
 					ad.title = item.getString("title");
 					ad.hasAdUrl = item.getString("hasAdUrl");
 					ads.add(ad);
				}
 				mAdbarResultModel.ads = ads;
			}catch (Exception e) {
				// TODO: handle exception
			}
			mAd_bar.ad_bar_callback(mAdbarResultModel);
			break;
		case GlobalSetting.CLICK_AD_BAR:
			mClickAdResultModel = new ClickAdResultModel();
			try{
				JSONObject json = new JSONObject(result_code);
				mClickAdResultModel.resultCode = json.getString("resultCode");
				mClickAdResultModel.resultMsg = json.getString("resultMsg");
				if(json.has("url")){
					mClickAdResultModel.url = json.getString("url");
				}
			}catch (Exception e) {
				// TODO: handle exception
			}
			mClick_ad_listener.click_ad_callback(mClickAdResultModel);
			break;
		default:
			break;
		}
	}

}
